package mest.controller.first;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import mest.VO.first.addProductInfo.save_provider_productJobVO;
import mest.VO.first.firstVO;
import mest.entity.product_job;
import mest.entity.product_order;
import mest.entity.plan_purchase;
import mest.service.first.IOrderService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import mest.entity.save_file;
import javax.annotation.Resource;
import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @auther Machenike
 * @create 2019-11-20 23:57
 */
@Controller
@CrossOrigin
@RequestMapping(value = "/Order")
public class Order {

    @Resource
    private IOrderService ios;

    @RequestMapping(value = "/findProductOrderNumber")
    @ResponseBody
    public String findProductOrderNumber(){
        Integer productOrderNumber = ios.findProductOrderNumber();
        return productOrderNumber.toString();
    }

    @RequestMapping(value = "/selectProductInfo")
    @ResponseBody
    public String selectProductInfo(int index,int pageSize){
        List<save_provider_productJobVO> save_provider_productJobVOList = ios.selectProductInfo(index,pageSize);
        Map map  = new HashMap();
        int i = ios.selectProductInfoCount();
        map.put("code",0);
        map.put("msg","");
        map.put("count",i);
        map.put("data",save_provider_productJobVOList);
        return JSON.toJSONString(map);
    }
    @RequestMapping(value = "/addOrder")
    @ResponseBody
    public String addOrder(String productOrder,String saveList){

        product_order product_order = JSON.parseObject(productOrder, product_order.class);
        System.out.println(product_order);

        //后端设置时间
        product_order.setTime(new Date(System.currentTimeMillis()));
        //默认设置下单人为1，暂时没有session
        product_order.setOrderPlacerId(1);
        //插入语句
        ios.insertProductOrder(product_order);

        System.out.println(product_order.getId());
        List<save_provider_productJobVO> spJobVO = JSONArray.parseArray(saveList, save_provider_productJobVO.class);


        //去空处理三步骤
        List listNull = new ArrayList();
        listNull.add(null);
        spJobVO.removeAll(listNull);

    //设置poId,生产单的Id
        for(int i = 0 ; i < spJobVO.size(); i++){
            if(spJobVO.get(i) != null)
                spJobVO.get(i).setPoId(product_order.getId());
        }
        int i = ios.insertProductJob(spJobVO);

        System.out.println(spJobVO.size());
        System.out.println(spJobVO);
        if(i == spJobVO.size())
            return "添加成功";
        else
            return "添加失败";
    }

    //首页所有生产单列表
    @RequestMapping(value = "/allOrder")
    @ResponseBody
    public String allOrder(int index,int pageSize){
        List<firstVO> orderList = ios.findAllOrder(index,pageSize);
        Map map  = new HashMap();
        int i = ios.findAllOrderCount();
        map.put("code",0);
        map.put("msg","");
        map.put("count",i);
        map.put("data",orderList);
        return JSON.toJSONString(map);
    }


    //审核通过
    @RequestMapping(value="/check")
    @ResponseBody
    public String check(plan_purchase pp){


        int check = ios.check(pp);
        return check==1 ?"成功":"失败";
    }
}
